package rsa;

import java.applet.Applet;

import java.awt.Button;
import java.awt.Font;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

public class RSADemo extends Applet implements ActionListener {
	/**
	 * Eclipse wants us to use this serial number. Alright then.
	 */
	private static final long serialVersionUID = -3392944933930829806L;

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == ButtonCalculate) {
			try {
				p = new BigInteger(FieldPrimeP.getText());
				q = new BigInteger(FieldPrimeQ.getText());
			} catch (NumberFormatException e) {
				FieldVarD.setText("Kon getallen niet herkennen.");
				return;
			}
			
			// calculate the other variables
			n = p.multiply(q);
			FieldVarN.setText(n.toString());
			fi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
			e = Util.CalculateE(fi);
			d = e.modInverse(fi);
			
			FieldVarD.setText(d.toString());
			FieldVarE.setText(e.toString());
			return;
		}
		if (event.getSource() == ButtonEncrypt) {
			BigInteger x = Util.convert(FieldMessage.getText());
			e = new BigInteger(FieldVarE.getText());
			n = new BigInteger(FieldPrimeP.getText()).multiply(new BigInteger(FieldPrimeQ.getText()));
			y = x.modPow(e, n);
			FieldEncrypted.setText(y.toString());
			return;
		}
		if (event.getSource() == ButtonDecrypt) {
			d = new BigInteger(FieldVarD.getText());
			n = new BigInteger(FieldPrimeP.getText()).multiply(new BigInteger(FieldPrimeQ.getText()));
			BigInteger x = y.modPow(d, n);
			FieldMessage.setText(Util.revert(x));
			return;
		}
	}
	
	private TextField FieldPrimeP = new TextField("p: Priemgetal 1");
	private TextField FieldPrimeQ = new TextField("q: Priemgetal 2");
	private TextField FieldVarD = new TextField("d: Decodeersleutel");
	private TextField FieldVarE = new TextField("e: Encodeersleutel");
	private TextField FieldVarN = new TextField("n: Sleutel");
	private TextField FieldMessage = new TextField("x: voorbeeldtekst");
	private TextField FieldEncrypted = new TextField("y: versleutelde tekst");
	private Button ButtonCalculate = new Button("Bereken!");
	private Button ButtonEncrypt = new Button("Encodeer!");
	private Button ButtonDecrypt = new Button("Decodeer!");
	
	private BigInteger p;
	private BigInteger q;
	private BigInteger n; 
	private BigInteger fi; 
	private BigInteger e; 
	private BigInteger d;
	private BigInteger y;
	
	public void init() {
		setFont(new Font("Courier New", Font.PLAIN, 12));
		setSize(800, 600);
		
		Panel cp = new Panel(null);
		cp.setBounds(0, 0, 800, 600);
		add(cp);
		
		FieldMessage.setBounds(24, 20, 752, 18);
		cp.add(FieldMessage);
		FieldEncrypted.setBounds(24, 50, 752, 18);
		cp.add(FieldEncrypted);
		
		FieldPrimeP.setBounds(24, 120, 752, 18);
		cp.add(FieldPrimeP);
		FieldPrimeQ.setBounds(24, 150, 752, 18);
		cp.add(FieldPrimeQ);
		
		FieldVarD.setBounds(24, 180, 752, 18);
		cp.add(FieldVarD);
		FieldVarE.setBounds(24, 210, 752, 18);
		cp.add(FieldVarE);
		
		FieldVarN.setBounds(24, 240, 752, 18);
		cp.add(FieldVarN);
		
		ButtonCalculate.setBounds(24, 500, 200, 24);
		ButtonCalculate.addActionListener(this);
		cp.add(ButtonCalculate);
		ButtonEncrypt.setBounds(24, 552, 200, 24);
		ButtonEncrypt.addActionListener(this);
		cp.add(ButtonEncrypt);
		ButtonDecrypt.setBounds(248, 552, 200, 24);
		ButtonDecrypt.addActionListener(this);
		cp.add(ButtonDecrypt);
	}
}
